const Order = require('../model/order');

module.exports = {

  /**
   * 添加订单的中间件
   * @param request
   * @param response
   * @param next
   */
  sOrder: (request, response, next) => {
    let {code, user_username, time, amount} = request.body

    // console.log('MID--------order_code = ' + order_code + '; user_username = ' + user_username +
    //   '; time = ' + time + '; amount = ' + amount + ';-------------------');
    let order = {
      code, user_username, time, amount
    }

    Order.setOrder(order)
      .then(result => {
        request.add_oid = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定订单的条数
   * @param request
   * @param response
   * @param next
   */
  gOrderCount: (request, response, next) => {
    let {username, code, lessAmount, moreAmount, lessDate, moreDate} = request.body

    Order.getOrderCount(username, code, lessAmount, moreAmount, lessDate, moreDate)
      .then(result => {
        request.orderCount = result
        next()
      })
      .catch(error => {
        next(error)
      })

  },

  /**
   * 获取指定的订单信息的中间件
   * @param request
   * @param response
   * @param next
   */
  gOrder: (request, response, next) => {
    let start = response.start
    let size = response.size
    let {username, code, lessAmount, moreAmount, lessDate, moreDate} = request.body

    Order.getOrder(start, size, username, code, lessAmount, moreAmount, lessDate, moreDate)
      .then(result => {
        request.recordCountOrder = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }
}
